#include	"LogService.h"
#include	"LogFile.h"
#include	<protocols/Common.pb.h>

void LogService::OnReceive(Sender * sender, Protocol * msg) {
	if (msg->pf == Proto::CMD_LOG) {
		auto cmd = Cast<Proto::CmdLog>(msg);
		if (cmd) {
			auto it = _logs.find(cmd->action());
			if (it == _logs.end()) {
				LogFile * file = new LogFile(cmd->action(), 16 * 1024 * 1024);
				if (!file) {
					LOG_ERR("[logserver]Failed to write action log due to alloc");
				} else {
					_logs[cmd->action()] = file;
					file->Write(cmd->time(), cmd->pid(), cmd->detail());
				}
			} else {
				it->second->Write(cmd->time(), cmd->pid(), cmd->detail());
			}
		}
	} else {
		LOG_ERR("[logserver]Unsupported protocol received.");
	}
}